package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.PageResult;
import com.pinyougou.mapper.SpecificationMapper;
import com.pinyougou.mapper.SpecificationOptionMapper;
import com.pinyougou.pojo.Specification;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service(interfaceName="com.youpingou.service.SpecificationService")
@Transactional
public class SpecificationServiceImpl implements SpecificationService
{
    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    /**
     * 多条件分页查询
     *
     * @param specification
     * @param page
     * @param rows
     */

    @Override
    public PageResult findByPage(Specification specification,int page,int rows)
    {
        PageInfo<Specification> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect()
        {
            @Override
            public void doSelect()
            {
                specificationMapper.findByPage(specification);
            }
        });
        return new PageResult(pageInfo.getPages(),pageInfo.getList());
    }
    /** 查找规格id,name */
    @Override
    public List<Map<String, Object>> findIdNameList()
    {
        return specificationOptionMapper.findIdNameList();
    }


    /**
     * 添加方法
     *
     * @param specification
     */
    @Override
    public void save(Specification specification)
    {
        //insertSelective会先查询表中的id，再赋值给specification
        specificationMapper.insertSelective(specification);
        specificationOptionMapper.insertList(specification);
    }

    /** 根据规格id查询规格选项 */
    @Override
    public List<SpecificationOption> findOptionList(Long id)
    {
        SpecificationOption specificationOption = new SpecificationOption();
        specificationOption.setSpecId(id);
        return specificationOptionMapper.select(specificationOption);
    }

    /**
     * 修改方法
     *
     * @param specification
     */
    @Override
    public void update(Specification specification)
    {
        //1.首先修改主表的
        specificationMapper.updateByPrimaryKeySelective(specification);
        //2.再修改从表的id
        SpecificationOption specificationOption = new SpecificationOption();
        specificationOption.setSpecId(specification.getId());
        //2.1 先删除以主表字段id为外键的记录
        specificationOptionMapper.delete(specificationOption);
        //2.2 再添加从表的新纪录
        specificationOptionMapper.insertList(specification);

    }

    /**
     * 根据主键id删除
     *
     * @param id
     */
    @Override
    public void delete(Serializable id)
    {

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteAll(Serializable[] ids)
    {
        //先删除从表tb_specification_option中的数据
        //delete from tb_specification_option
        Example example = new Example(SpecificationOption.class);
        //创建条件对象
        Example.Criteria criteria = example.createCriteria();
        //spec_id in (?,?,?)
        criteria.andIn("specId", Arrays.asList(ids));
        //执行删除
        specificationOptionMapper.deleteByExample(example);

        //再删除主表tb_specification中的数据
        example = new Example(Specification.class);
        criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(ids));
        specificationMapper.deleteByExample(example);

    }

    /**
     * 根据主键id查询
     *
     * @param id
     */
    @Override
    public Specification findOne(Serializable id)
    {
        return null;
    }

    /**
     * 查询全部
     */
    @Override
    public List<Specification> findAll()
    {
        return null;
    }

}